<div class="row-fluid">
	<div class="span12">
		<ul class="breadcrumb">
			<li>
				<a href="index.php">
					<i class="radmin-icon radmin-home"></i>
					Dashboard
				</a>
				<span class="divider">/</span>
			</li>
			<li>
				<a href="whm-accounts.php">
					<i class="radmin-icon radmin-clipboard-2"></i>
					WHM Accounts
				</a>
				<span class="divider">/</span>
			</li>
			<li class="active">
				<i class="radmin-icon radmin-clipboard"></i>
				cPanel Accounts
			</li>
		</ul>
	</div>
</div>

<div class="row-fluid">
	<div class="span12">
		<h4 class="title"><?php $this->widget('application.components.mywidgets.WikiHelp', array('wiki' => 'cpanel_accounts_terminate')); ?> cPanel Accounts | Terminate Accounts</h4>
		<div class="squiggly-border"></div>

		<fieldset>
			<?php echo CHtml::dropdownList('id_cpanel_accounts', '', $cpanelaccountsDataSource, array('empty' => $cpanelaccountsCount.' Accounts to Terminate')); ?><br />
			<label class="checkbox">
				<?php echo CHtml::checkBox('keepdns'); ?>
				Keep DNS
			</label>
			<label class="checkbox text-warning">
				<?php echo CHtml::checkBox('understand'); ?>
				I understand this will <strong>irrevocably remove</strong> all above listed accounts. Besides, the whm-accounts with same username as the accounts will be removed together.
			</label>
			<button class="btn btn-small btn-primary" type="button" id="cpanel_terminate_btn">Terminate Accounts</button><br /><br />
			<div class="row-fluid">
				<div class="span12 <?php echo empty($denyTerminateAccount)? 'hide' : ''; ?>" id="cpanelterminate_info">
					<div class="alert alter-block alert-info">
			<!--			<button type="button" class="close" data-dismiss="alert">&times;</button>-->
						<?php if(!empty($denyTerminateAccount)): ?>
							<h4>IMPORTANT</h4>
							<p>The owner of these cpanel accounts <?php echo $denyTerminateAccount->domain; ?> do not have terminatereseller permission and will most likely fail this request</p>
						<?php endif; ?>
						<div class="content"></div>
					</div>
				</div>
			</div>
			
			<script>
				var terminateRequest = function(){
					this.requestUrl = '';
					this.id_cpanel_accounts = null;
					this.keepdns = null;

					this.beforeRequest = function(id_cpanel_account, doTimes){};
					this.requestCallback = function(id_cpanel_account, serverReturn, doTimes){};
					this.afterDone = function(){};

					this.doTimes = 0;
					this.do = function(){
						this.doTimes++;
						var id_cpanel_account = this.id_cpanel_accounts.pop();
						if(!id_cpanel_account){
							this.afterDone();
							return false;
						}
						
						this.beforeRequest(id_cpanel_account, this.doTimes);
						var thisObj = this;
						$.ajax({
							type: 'post',
							url: thisObj.requestUrl,
							data: {
								id_cpanel_account: id_cpanel_account,
								keepdns: thisObj.keepdns
							},
							success: function(ret){
								thisObj.requestCallback(id_cpanel_account, ret, thisObj.doTimes);
								thisObj.do();
								return true;
							},
							dataType: 'json'
						});
					};
				};
				
				$('#cpanel_terminate_btn').click(function(){
					var understand = $('#understand').is(':checked');
					if(!understand){
						$('#cpanelterminate_info').blink({
							'beforeCall': function(){
								$('#cpanelterminate_info .content').append(
									'<p><b>Please tick the "understand" option</b></p>'
								);
							},
							'showTime': 0
						});
						return false;
					}
					var keepdns = $('#keepdns').is(':checked')? 1 : 0;
					var id_cpanel_accounts = [];
					if($('#id_cpanel_accounts').val() == ''){
						$('#id_cpanel_accounts option[value!=""]').each(function(k, v){
							id_cpanel_accounts.push($(v).val());
						});
					}else{
						id_cpanel_accounts.push($('#id_cpanel_accounts').val());
					}

					$('#cpanelterminate_info').blink({
						'beforeCall': function(){
							$('#cpanelterminate_info .content').append(
								'<p><b>Status ('+id_cpanel_accounts.length+' Account)</b></p>'
							);
						},
						'callBack': function(){
							var terminateRequester = new terminateRequest();
							var length = id_cpanel_accounts.length;
							terminateRequester.keepdns = keepdns;
							terminateRequester.id_cpanel_accounts = id_cpanel_accounts;

							terminateRequester.requestUrl = <?php echo json_encode(url('whm/cpanelaccount/terminate')); ?>;
							terminateRequester.beforeRequest = function(id_cpanel_account, doTimes){
								$('#cpanel_terminate_btn').button('loading');
								$('#cpanelterminate_info .content').append(
									'<p>'+$('#id_cpanel_accounts option[value="'+id_cpanel_account+'"]').text()+': Waiting...'+' ('+doTimes+' of '+length+' Accounts)</p>'
								);
							};
							terminateRequester.afterDone = function(){
								$('#cpanel_terminate_btn').button('reset');
							}
							terminateRequester.requestCallback = function(id_cpanel_account, serverReturn, doTimes){
								if(serverReturn.result){
									var username = $('#id_cpanel_accounts option[value="'+id_cpanel_account+'"]').text();
									if(serverReturn.terminateresult.status == 1){
										var result = '<p class="text-success accordion-toggle" data-toggle="collapse" data-target="#'+id_cpanel_account+serverReturn.time+'">&nbsp;&nbsp;&nbsp;&nbsp; <b>'+username+' - Success</b> Show/Hide Raw Output</p>';
									}else{
										var result = '<p class="text-error accordion-toggle" data-toggle="collapse" data-target="#'+id_cpanel_account+serverReturn.time+'">&nbsp;&nbsp;&nbsp;&nbsp; <b>'+username+' - Failed</b> Show/Hide Raw Output</p>';
									}
									$('#cpanelterminate_info .content').append(result);
									$('#cpanelterminate_info .content').append('<p id="'+id_cpanel_account+serverReturn.time+'" class="muted collapse out">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b>'+($.isEmptyObject(serverReturn.terminateresult.rawout)? serverReturn.terminateresult.statusmsg : serverReturn.terminateresult.rawout)+'</b></p>');
								}else{
									$.each(serverReturn.errors, function(attrName, error){
										$(error).each(function(k, e){
											$('#cpanelterminate_info .content').append(
												'<p class="text-error">&nbsp;&nbsp;&nbsp;&nbsp; <b>'+$('#id_cpanel_accounts option[value="'+id_cpanel_account+'"]').text()+' - '+e+' ('+doTimes+' of '+length+' Accounts)</b></p>'
											);
										});
									});
								}
							};
							terminateRequester.do();
						},
						'showTime': 0
					});
					
				});
			</script>
		</fieldset>
		
	</div>
</div>